﻿using System;
using System.Security.OAuth;
using System.Windows.Forms;
using Fiddler;
using Fiddler2.OAuth;

/// <summary>
/// Allows for easy OAuth signed request via Fiddler2
/// </summary>
public class OAuthTamper : IAutoTamper
{
    private bool autoAuth = false;
    private OAuthManager manager = new OAuthManager();
    private MenuItem optionsMenuItem;
    private MenuItem contextMenuItem;

    #region IAutoTamper Members

    public void AutoTamperRequestAfter(Session oSession)
    {
        if (autoAuth && !oSession.oRequest.headers.Exists("Authorization"))
        {
            oSession.oRequest.headers.Add("Authorization", manager.GenerateAuthorizationHeader(oSession.fullUrl, oSession.oRequest.headers.HTTPMethod));
        }
    }

    public void AutoTamperRequestBefore(Session oSession)
    {
        // DO NOTHING
    }

    public void AutoTamperResponseAfter(Session oSession)
    {
        // DO NOTHING
    }

    public void AutoTamperResponseBefore(Session oSession)
    {
        // DO NOTHING
    }

    public void OnBeforeReturningError(Session oSession)
    {
        // DO NOTHING
    }

    #endregion

    #region IFiddlerExtension Members

    public void OnBeforeUnload()
    {
        // TODO: Save OAuth settings
    }

    public void OnLoad()
    {

#if DEBUG
        FiddlerApplication.Prefs.SetBoolPref("fiddler.debug.extensions.showerrors", true);
        FiddlerApplication.Prefs.SetBoolPref("fiddler.debug.extensions.verbose", true);
#endif

        optionsMenuItem = new MenuItem("OAuth Options");
        optionsMenuItem.Click += new EventHandler(optionsMenuItem_Click);
        FiddlerApplication.UI.mnuTools.MenuItems.Add(optionsMenuItem);

        contextMenuItem = new MenuItem("Reissue with OAuth");
        contextMenuItem.Click += new EventHandler(contextMenuItem_Click);
        FiddlerApplication.UI.mnuSessionContext.MenuItems.Add(contextMenuItem);

        // TODO: Read saved OAuth settings
    }

    #endregion

    private void contextMenuItem_Click(object sender, EventArgs e)
    {
        foreach (Session oSession in FiddlerApplication.UI.GetSelectedSessions(1))
        {
            if (oSession.oRequest.headers.Exists("Authorization"))
            {
                oSession.oRequest.headers.Remove("Authorization");
            }
            oSession.oRequest.headers.Add("Authorization", manager.GenerateAuthorizationHeader(oSession.fullUrl, oSession.oRequest.headers.HTTPMethod));
            FiddlerApplication.DoComposeByCloning(oSession);
        }
    }

    private void optionsMenuItem_Click(object sender, EventArgs e)
    {
        oAuthUI ui = new oAuthUI();
        ui.Token = manager.Token;
        ui.TokenSecret = manager.TokenSecret;
        ui.ConsumerToken = manager.ConsumerKey;
        ui.ConsumerSecret = manager.ConsumerSecret;
        ui.AutoOAuth = autoAuth;
        if (ui.ShowDialog() == DialogResult.OK)
        {
            manager.Token = ui.Token;
            manager.TokenSecret = ui.TokenSecret;
            manager.ConsumerKey = ui.ConsumerToken;
            manager.ConsumerSecret = ui.ConsumerSecret;
            autoAuth = ui.AutoOAuth;
        }
    }
}
